package utils;

import java.sql.Date;
import java.util.List;
import java.util.StringTokenizer;

import processing.core.PApplet;
import processing.core.PVector;

public class Utils {

	public static String[] getArrayFromString(String s, String delim){
		StringTokenizer token = new StringTokenizer(s, delim);
		String[] array = new String[token.countTokens()];
		int i = 0;
		while(token.hasMoreTokens()){
			array[i] = token.nextToken();
			i++;
		}
		/*for (int j = 0; j < array.length; j++) {
			System.out.println(array[j]);
		}
		System.out.println("----");*/
		return array;
	}
	
	public static String getStringFromArray(String[] s, String delim){
		String ret = "";
		for (int i = 0; i < s.length; i++) {
			ret = ret + s[i] + delim;
		}
		return ret;
	}

	public static String[] concatenateArrays(String[] a1, String[] a2) {
		String[] concat = new String[a1.length + a2.length];
		int it = 0;
		for (int i = 0; i < a1.length; i++) {
			concat[it] = a1[i];
			it++;
		}
		for (int i = 0; i < a2.length; i++) {
			concat[it] = a2[i];
			it++;
		}
		return concat;
	}

	public static int random(int limit){
		return (int) Math.floor(Math.random()*100%limit);
	}
	
//	public static void copyFile(String file1, String file2){
//		Arquivo arq = new Arquivo(file1,file2);
//		while(!arq.isEndOfFile()){
//			arq.println(arq.readLine());
//		}
//	}
	
	public static String getTimeStamp(){
		Date date = new Date(System.currentTimeMillis());
		String dateS = date.toLocaleString();
	    dateS = dateS.replaceAll("/", "");
		dateS = dateS.replaceAll(" ", "");
		dateS = dateS.replaceAll(":", "");
		return dateS;
	}
	
	public static float map(float arg0, float arg1, float arg2, float arg3, float arg4){
		return PApplet.map(arg0, arg1, arg2, arg3, arg4);
	}
	
	public static void copyPoints(List<PVector> origem, List<PVector> destino){
		destino.clear();
		for (PVector pVector : origem) {
			destino.add(new PVector(pVector.x, pVector.y));
		}
	}
	
	public static PVector getCentralPoint(List<PVector> points){
		PVector result = null;
		float maxX = 0;
		float maxY = 0;
		float minX = 999;
		float minY = 999;
		for (PVector p : points) {
			float curX = p.x;
			float curY = p.y;
			if(curX > maxX){
				maxX = curX;
			}
			if(curX < minX){
				minX = curX;
			}
			if(curY > maxY){
				maxY = curY;
			}
			if(curY < minY){
				minY = curY;
			}
		}
		float tamX = maxX - minX;
		float tamY = maxY - minY;
		result = new PVector(minX + (tamX/2), minY + (tamY/2));
		return result;
	}

}
